package pers.ree.demo.io.nio;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class ZsjFileUtils {

    public static String filePath = "/Users/rui/Documents/temp/zsj-1.txt";

    public static String outputFilePath = "/Users/rui/Documents/temp/zsj-1-out.txt";


    public static void main(String[] args) {
//        FileInputStream fin = null;

//        Map<String, Integer> prefixMap = new HashMap<String, Integer>();
//        prefixMap.put("境外普通住宅", 0);
//        prefixMap.put("境内商贸", 1);
//        prefixMap.put("境内写字楼", 2);
//        prefixMap.put("境内普通住宅", 3);

        String[] prefixArr = {"境外普通住宅", "境内商贸", "境内写字楼", "境内普通住宅", "其他类型"};

        int size = prefixArr.length;

        List<String []> result = new ArrayList<String[]>();

        BufferedWriter bfw = null;

        try {
            List<String> lines = Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8);
//            StringBuilder sb = new StringBuilder();

            String[] readLine = null;
            String[] lineResult = null;

            for (String line : lines) {
//                sb.append(line+"\n");//  \r\n  换行符

                if (line == null || line.trim().length() == 0) {
                    continue;
                }

                readLine = line.split(",");
                if (readLine == null || readLine.length == 0) {
                    continue;
                }

//                lineResult = Arrays.copyOf(prefixArr, prefixArr.length);
                lineResult = new String[size];
                result.add(lineResult);

                for (int i=0, l=readLine.length; i<l; i++) {
                    for (int j=0; j < size; j++) {
                        if (readLine[i].contains(prefixArr[j])) {
                            lineResult[j] = readLine[i];
                        }
                    }
                }
            }

//            String fromFile = sb.toString();
//            System.out.println(fromFile);
//            String col = null;
//            for (String[] line: result) {
//
//                if (line == null || line.length == 0) {
//                    continue;
//                }
//
//                for (int i=0; i<line.length; i++) {
//                    if ((col=line[i]) == null || col.trim().length() == 0) {
//                        System.out.print(0);
//
//                    } else {
//                        System.out.print(col.trim());
//                    }
//
//                    if (i != line.length-1) {
//                        System.out.print(",");
//                    } else {
//                        System.out.println("");
//                    }
//                }
//            }


            Path outputFilePathObj = Paths.get(outputFilePath);
            //创建文件
            if(!Files.exists(outputFilePathObj)) {
                try {
                    Files.createFile(outputFilePathObj);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            bfw = Files.newBufferedWriter(outputFilePathObj);
//            bfw.write("Files类的API:newBufferedWriter");

            String col = null;
            for (String[] line: result) {

                if (line == null || line.length == 0) {
                    continue;
                }

                for (int i=0; i<line.length; i++) {
                    if ((col=line[i]) == null || col.trim().length() == 0) {
//                        System.out.print(0);
                        bfw.write("0");

                    } else {
//                        System.out.print(col.trim());
                        bfw.write(col.trim());
                    }

                    if (i != line.length-1) {
                        bfw.write(",");
                    } else {
                        bfw.write("\n");
                    }
                }
            }

            bfw.flush();
            bfw.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bfw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
